//****************************************************
// Handles all of the spellchecking in scribefire.
//
// TODO: Make it spellcheck text that has been loaded
//       from last time right away, instead of
//       waiting for it to change first.
//****************************************************

function sfSpellCheck()
{
	this.prefs = null;
}

sfSpellCheck.prototype.init = function(theTextBox)
{
	//load the javascript loader
	var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader);
	
	//use the above javascript loader to load the provided spellcheck wrapper
	loader.loadSubScript("chrome://global/content/inlineSpellCheckUI.js", this);
	
	//put the firefox-wide prefs into an object
	//used to get local when adding more dictionaries
	this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
	
	var doSpellCheck = (this.prefs.getIntPref("layout.spellcheckDefault") >= 1);
	
	if (doSpellCheck) {
		this.InlineSpellCheckerUI.init(theTextBox);
		this.InlineSpellCheckerUI.enabled = true;
	}
	else {
		this.hideContextSpellingItems();
	}
}

//generates and handles the context menu items (spelling suggestions, etc.)
//popupNode is the context menu with which to work
sfSpellCheck.prototype.initContextMenu = function(popupNode)
{
	//if the spellcheck wrapper says it can't run, hide the context items and leave
	if (!this.InlineSpellCheckerUI.canSpellCheck)
	{
		this.hideContextSpellingItems();
		return;
	}

	//pass the context menu to the wrapper
	this.InlineSpellCheckerUI.initFromEvent(document.popupRangeParent, document.popupRangeOffset);
	
	//set the context-menu item for toggling spell-check to the enabled var from the wrapper
	document.getElementById("spell-check-enabled").setAttribute("checked", this.InlineSpellCheckerUI.enabled);

	//set var wordMisspelled to the wrapper's var
	//this var is true if the context menu was raised when the mouse was over a misspelled word
	var wordMisspelled = this.InlineSpellCheckerUI.overMisspelling;
	
	//toggle the spelling suggestions and the add-to-dictionary items depending on overMisspelling
	this.setElementVisibility("spell-add-to-dictionary", wordMisspelled);
	this.setElementVisibility("spell-suggestions-separator", wordMisspelled);

	//set the number of suggestions
	var numsug = this.InlineSpellCheckerUI.addSuggestionsToMenu(popupNode, document.getElementById("spell-suggestions-separator"), 5);

	//set the visibility of the nosuggestions item
	this.setElementVisibility("spell-no-suggestions", (wordMisspelled && numsug == 0));

	//set the number of dictionaries
	var numdicts = this.InlineSpellCheckerUI.addDictionaryListToMenu(document.getElementById("spell-dictionaries-menu"), null);
	
	//set the visibility of the dictionary-changer as long as there are more than one dictionary to change between
	this.setElementVisibility("spell-dictionaries", (this.InlineSpellCheckerUI.enabled && numdicts > 1));
}

//clears the dictionaries and spelling suggestions from the context menu
sfSpellCheck.prototype.clearContextMenu = function(popupNode)
{
	if (this.InlineSpellCheckerUI)
	{
		this.InlineSpellCheckerUI.clearSuggestionsFromMenu();
		this.InlineSpellCheckerUI.clearDictionaryListFromMenu();
	}
}

//sets the visibility of an element by elementID
sfSpellCheck.prototype.setElementVisibility = function(elementID, visible)
{
	try
	{
		document.getElementById(elementID).hidden = !visible;
	} catch(e){}
}

//hides the spelling-related context-menu items
sfSpellCheck.prototype.hideContextSpellingItems = function()
{
	this.setElementVisibility("spell-no-suggestions", false);
	this.setElementVisibility("spell-check-enabled", false);
	this.setElementVisibility("spell-check-separator", false);
	this.setElementVisibility("spell-add-to-dictionary", false);
	this.setElementVisibility("spell-suggestions-separator", false);
	this.setElementVisibility("spell-dictionaries", false);
}

sfSpellCheck.prototype.addToDictionary = function()
{
	try
	{
		this.InlineSpellCheckerUI.addToDictionary();
	} catch(e){}
}

sfSpellCheck.prototype.toggleEnabled = function()
{
	try
	{
		this.InlineSpellCheckerUI.toggleEnabled();
	} catch(e){}
}

//handles the web-page opening for adding dictionaries depending on the version
sfSpellCheck.prototype.addDictionaries = function()
{
	//set the uri value as a url
	var formatter = Components.classes["@mozilla.org/toolkit/URLFormatterService;1"].getService(Components.interfaces.nsIURLFormatter);
	var uri = formatter.formatURLPref("browser.dictionaries.download.url");

	//set the locale as not null, then try and get it from Firefox
	var locale = "-";
	try
	{
		locale = this.prefs.getComplexValue("intl.accept_languages", Components.interfaces.nsIPrefLocalizedString).data;
	} catch(e){}

	//set the version as not null, then try and get it from Firefox
	var version = "-";
	try
	{
		version = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo).version;
	} catch(e){ }

	//add the proper locale and version to the uri
	uri = uri.replace(/%LOCALE%/, escape(locale));
	uri = uri.replace(/%VERSION%/, version);

	//prepare to open it in a new tab
	var newWindowPref = this.prefs.getIntPref("browser.link.open_newwindow");
	var where = newWindowPref == 3 ? "tab" : "window";
	
	var browser = getBrowser();
	window.top.browser.selectedTab = window.top.browser.addTab(uri);
}
